/*********************************************************
          File Name:16.6.cpp
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Sat 12 Dec 2015 01:46:13 PM CST
**********************************************************/

// NOTE 
// 1. when `N` is 0 then `nm::end()` == `nm::begin()`   
// 2. this test program ignored this case 

#include <iostream>
#include <string>
#include <vector>

using namespace std;

namespace nm
{
        template<typename C>
        auto begin(C &c) -> decltype(c.begin())
        {
                cout << " C ";
                return c.begin();
        }

        template<typename T, size_t N>
        T *begin(T (&arr)[N])
        {
                cout << "T [N] ";
                return arr;
        }

        template<typename T, size_t N>
        T *end(T (&arr)[N])
        {
                return arr + N;         // may larger then arr size
        }
}

int main(void)
{
        int a[] = {1, 2, 3};
        char b[] = {'a', 'b', 'c'};
        const char *c[] = {"aa", "bb", "cc"};
//        const char *d = "abc";
//        string s = "abc";
        string ss[] = {"aa", "bb", "cc"};
        vector<int> v{1, 2, 3};
        vector<string> vs{"aa", "bb", "cc"};

        cout << "beg " << *nm::begin(a) << "\tend " << *(nm::end(a) - 1)<< endl;
        cout << "beg " << nm::begin(b) << "\tend " << (nm::end(b) - 1) << endl;
        cout << "beg " << *nm::begin(c) << "\tend " << *(nm::end(c) -1) << endl;
//        cout << "beg " << std::begin(d) << endl;
//        cout << "beg " << nm::begin(s) << endl;
        cout << "beg " << *nm::begin(ss) << "\tend " << *(nm::end(ss) - 1)<< endl;
}
